package com.wc.fortnight_blue_bridge.Q250405.破译密码;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/4/5 20:07
 * @description
 * https://www.lanqiao.cn/problems/20288/learning/?contest_id=253
 */
public class Main {
    /**
     * 贪心，推导
     * 假设 任务 1  a b, 任务2 c d
     * 先任务 1, a + b + max(0, c - b) + d
     * 先任务 2, c + d + max(0, a - d) + b
     * 可以以这个排序 谁时间短就往前面排,
     * 然后卡点是, 相等的时候, 需要破译时间短的往前
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    static int[][] a = new int[N][2];
    static boolean[] st = new boolean[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i][0] = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i][1] = sc.nextInt();
        Arrays.sort(a, 1, n + 1, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                int a = o1[0], b = o1[1], c = o2[0], d = o2[1];
                int x = a + Math.max(0, c - b);
                int y = c + Math.max(0, a - d);
                if (x == y) return o1[0] - o2[0];
                return x - y;
            }
        });
//        int res = 0, rst = 0, i = 1, j = 1;
//        while (i <= n || j <= n) {
//            // 该完成的任务
//            if (i <= n) {
//                // 如果前面有时间
//                if (rst > 0) {
//                    // 用
//                    if (a[i][0] >= rst) {
//                        a[i][0] -= rst;
//                        rst = 0;
//                    } else {
//                        // 多余了, 那就不用时间了
//                        rst -= a[i][0];
//                        a[i][0] = 0;
//                    }
//                }
//                res += a[i][0];
//                st[i] = true;
//                i++;
//            }
//            // 无限的完成, 前面有完成的就传
//            while (j <= n && st[j]) {
//                res += a[j][1];
//                rst += a[j][1];
//                j++;
//            }
//        }
        // res 答案
        int res = 0, rst = 0;
        for (int i = 1; i <= n; i++) {
            // 如果前面有多的rst时间, 我们就可以利用
            if (a[i][0] >= rst) {
                a[i][0] = a[i][0] - rst;
                rst = 0;
            } else {
                rst -= a[i][0];
                a[i][0] = 0;
            }
            res += a[i][0] + a[i][1];
            rst += a[i][1];
        }

        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

